package stud.mpgame.physics;
import stud.mpgame.flash.map.GameMap;
import haxe.FastList;

/**
 * ...
 * @author pavel[f.pavel91@gmail.com]
 */
class PhysicsWorld {

	private var gravity:Vector2D;
	
	private var broadPhase:BroadPhase;
	private var circles:FastList< Circle >;
	
	public function new(gravity:Vector2D, gameMap:GameMap) {
		this.circles = new FastList< Circle >();
		
		this.gravity = gravity;
		this.broadPhase = new BroadPhase(gameMap);
	}
	
	public function addCircle(circle:Circle) {
		circles.add(circle);
		broadPhase.addCircle(circle);
	}
	
	public function removeCircle(circle:Circle) {
		circles.remove(circle);
		broadPhase.removeCircle(circle);
	}
	
	public function update(deltaTime:Int) {
		if (circles == null)
			return;
		//var circle:Circle = circles.first();	
		//trace("BEFORE" + circle.x + " " + circle.y);
		for (circle in circles) {
			//var dt:Float = deltaTime / 1000; //milliseconds -> seconds
			circle.move(circle.velocity.mul(deltaTime).addNew(gravity.mul(deltaTime * deltaTime / 2)));
			circle.velocity.add(gravity.mul(deltaTime));
		}
		//trace("AFTER" + circle.x + " " + circle.y);
		broadPhase.update();
	}
	
}